/*
 * ll2.c
 *
 *  Created on: 2015-04-04
 *      Author: root
 */


#include "ll2.h"



SNode *Node_createStep() {
    SNode *node = malloc(sizeof(SNode));
    //assert(node != NULL);

    node->data = 0;
    node->next = NULL;

    return node;
}


void Node_destroyStep(SNode *node) {
    //assert(node != NULL);
    free(node);
}


SList *List_createStep() {
    SList *list = malloc(sizeof(SList));
    //assert(list != NULL);

    SNode *node = Node_create();
    list->first = node;

    return list;
}


void List_destroyStep(SList *list) {
    //assert(list != NULL);

    SNode *node = list->first;
    SNode *next;
    while (node != NULL) {
        next = node->next;
        free(node);
        node = next;
    }

    free(list);
}


void List_appendStep(SList *list, int input)
{
    //assert(list != NULL);
    //assert(input != NULL);

    SNode *node = list->first;
    while (node->next != NULL) {
        node = node->next;
    }

    node->data = input;
    node->next = Node_create();
}


void List_insertStep(SList *list, int index, char *str) {
    //assert(list != NULL);
    //assert(str !=NULL);
    //assert(0 <= index);
    //assert(index <= List_length(list));

    if (index == 0) {
        SNode *after = list->first;
        list->first = Node_create();
        list->first->data = str;
        list->first->next = after;
    } else if (index == List_length(list)) {
        List_append(list, str);
    } else {
        SNode *before = list->first;
        SNode *after = list->first->next;
        while (index > 1) {
            index--;
            before = before->next;
            after = after->next;
        }
        before->next = Node_create();
        before->next->data = str;
        before->next->next = after;
    }
}


char *List_get(List *list, int index) {
    //assert(list != NULL);
    //assert(0 <= index);
    //assert(index < List_length(list));

    Node *node = list->first;
    while (index > 0) {
        node = node->next;
        index--;
    }
    return node->data;
}

/*
int List_find(List *list, char *str) {
    assert(list != NULL);
    assert(str != NULL);

    int index = 0;
    Node *node = list->first;
    while (node->next != NULL) {
        if (strlen(str) == strlen(node->data)) {
            int cmp = strcmp(str, node->data);
            if (cmp == 0) {
                return index;
            }
        }
        node = node->next;
        index++;
    }
    return -1;
}
*/


void List_removeStep(SList *list, int index) {
    //assert(list != NULL);
    //assert(0 <= index);
    //assert(index < List_length(list));

    if (index == 0) {
        SNode *node = list->first;
        list->first = list->first->next;
        SNode_destroy(node);
    } else {
        SNode *before = list->first;
        while (index > 1) {
            before = before->next;
            index--;
        }
        SNode *node = before->next;
        before->next = before->next->next;
        Node_destroy(node);
    }
}


int List_popStep(SList *list, int index) {
    //assert(list != NULL);
    //assert(0 <= index);
    //assert(index < List_length(list));

    if (index == 0) {
        SNode *node = list->first;
        list->first = list->first->next;
        int data = node->data;
        Node_destroy(node);
        return data;
    } else {
        SNode *before = list->first;
        while (index > 1) {
            before = before->next;
            index--;
        }
        SNode *node = before->next;
        before->next = before->next->next;
        int data = node->data;
        Node_destroy(node);
        return data;
    }
}


int List_lengthStep(SList *list) {
    //assert(list != NULL);

    SNode *node = list->first;
    int length = 0;
    while (node->next != NULL) {
        length++;
        node = node->next;
    }

    return length;
}


void List_print(SList *list) {
    //assert(list != NULL);

    //printf("[");
    bt_UARTsend("[");
	SNode *node = list->first;
    while (node->next != NULL) {
        //printf("%s", node->data);
        bt_UARTsend(itoa(node->data, 10));
    	node = node->next;
        if (node->next != NULL) {
            //printf(", ");
            bt_UARTsend(", ");
        }
    }
    //printf("]\n");
    bt_UARTsend("]\n");
}


